package com.sh.rbac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.rbac.core.common.annotation.SysLog;
import com.sh.rbac.domain.dto.role.*;
import com.sh.rbac.domain.entity.Role;
import com.sh.rbac.core.common.enums.OperationTypeEnum;
import com.sh.rbac.core.common.result.Result;
import com.sh.rbac.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色控制器
 *
 * @author lucky
 * @since 2024-04-07
 */

@Tag(name = "角色管理", description = "角色相关操作")
@RequestMapping("sys/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Operation(summary = "查询角色列表")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Role.class)))
    @GetMapping("list")
    @PreAuthorize("@ps.hasPerm('sys:role:list')")
    public Result<List<Role>> getList() {
        return Result.success(roleService.list());
    }

    @Operation(summary = "分页查询角色列表", description = "根据角色条件查询参数进行查询")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Role.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("@ps.hasPerm('sys:role:page')")
    public Result<Page<Role>> getPage(@PathVariable Long current, @PathVariable Long limit, @RequestBody RoleQueryDTO roleQueryDTO) {
        return Result.success(roleService.getPage(current, limit, roleQueryDTO));
    }

    @SysLog(module = "系统管理->角色管理", type = OperationTypeEnum.OTHER)
    @Operation(summary = "保存角色权限")
    @PostMapping("saveAuth")
    @PreAuthorize("@ps.hasPerm('sys:role:save-auth')")
    public Result<Boolean> saveAuth(@RequestBody RoleAuthDTO roleAuthDTO) {
        return roleService.saveAuth(roleAuthDTO) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统管理->角色管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加角色", description = "保存一个新角色")
    @ApiResponse(responseCode = "200", description = "成功保存角色")
    @PostMapping("save")
    @PreAuthorize("@ps.hasPerm('sys:role:save')")
    public Result<Boolean> save(@RequestBody RoleSaveDTO roleSaveDTO) {
        return roleService.save(roleSaveDTO) ? Result.success() : Result.failure();
    }


    @SysLog(module = "系统管理->角色管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新角色", description = "根据ID更新角色信息")
    @ApiResponse(responseCode = "200", description = "成功更新角色")
    @PutMapping("update")
    @PreAuthorize("@ps.hasPerm('sys:role:update')")
    public Result<Boolean> update(@RequestBody RoleUpdateDTO roleUpdateDTO) {
        return roleService.updateById(roleUpdateDTO) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统管理->角色管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除角色", description = "根据ID删除角色信息")
    @ApiResponse(responseCode = "200", description = "成功删除角色")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("@ps.hasPerm('sys:role:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return roleService.removeById(id) ? Result.success() : Result.failure();
    }


    @Operation(summary = "根据ID查询角色")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Role.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("@ps.hasPerm('sys:role:get')")
    public Result<Role> get(@PathVariable Long id) {
        return Result.success(roleService.getById(id));
    }


    @SysLog(module = "系统管理->角色管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除角色", description = "批量删除角色信息")
    @ApiResponse(responseCode = "200", description = "成功角色菜单")
    @DeleteMapping("batch-delete")
    @PreAuthorize("@ps.hasPerm('sys:role:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return roleService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统管理->角色管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新角色状态", description = "根据ID更新角色状态")
    @ApiResponse(responseCode = "200", description = "成功更新角色状态")
    @PutMapping("update-status/{id}/{status}")
    @PreAuthorize("@ps.hasPerm('sys:role:update-status')")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return roleService.updateStatus(id, status) ? Result.success() : Result.failure();
    }

}
